package com.rinvay.asdownloader.persistent;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.rinvay.network.filedownload.model.DownloadTask;
import com.rinvay.network.filedownload.model.Part;

import android.util.Log;


public class PartDataTool
{
    private static final String TAG = "PartDataTool";


    public static String buildPartsData(List<Part> parts) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("<parts uuid=\"" + parts.get(0).getResource().getUUId()
                + "\">");
        for (Part p : parts) {
            buffer.append("<part>")
                    .append("<begin>" + p.getBegin() + "</begin>")
                    .append("<size>" + p.getSize() + "</size>")
                    .append("<curlength>" + p.getCurLength() + "</curlength>")
                    .append("</part>");
        }
        buffer.append("</parts>");
        String result = buffer.toString();
        Log.i(TAG, "buildPartsData:" + result);
        return result;
    }

    public static void parsePartsData(DownloadTask task, String strPartsData) {
        String uuid = task.getUUId();
        List<Part> parts = task.getParts();

        XmlPullParserFactory factory;
        try {
            factory = XmlPullParserFactory.newInstance(); //XmlPullParserException   
            //            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();

            xpp.setInput(new StringReader(strPartsData));
            int eventType = xpp.getEventType();
            String tagname = "";
            int begin = 0;
            int size = 0;
            int curLen = 0;
            
            Part part = null;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_DOCUMENT) {
                } else if (eventType == XmlPullParser.START_TAG) {
                    tagname = xpp.getName();
                    if ("parts".equals(tagname)) {
                        String _uuid = xpp.getAttributeValue(null, "uuid");
                        if (!uuid.equals(_uuid)) {
                            Log.i(TAG, "uuid不匹配");
                            break;
                        }
                    }

                } else if (eventType == XmlPullParser.END_TAG) {
                    tagname = xpp.getName();
                    if ("part".equals(tagname)) {
                        part = new Part(task, begin, size, curLen);
                        parts.add(part);
                        part = null;
                    }
                    tagname = "";

                } else if (eventType == XmlPullParser.TEXT) {
                    if("begin".equals(tagname)) {
                        begin = Integer.parseInt(xpp.getText());
                    } else if("size".equals(tagname)) {
                        size = Integer.parseInt(xpp.getText());
                    } else if("curlength".equals(tagname)) {
                        curLen = Integer.parseInt(xpp.getText());
                    }
                }
                eventType = xpp.next(); //IOException
            }
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
